﻿// //   Copyright 2007-2011 Comdiv (F. Sadykov) - http://code.google.com/u/fagim.sadykov/
// //   Supported by Media Technology LTD 
// //    
// //   Licensed under the Apache License, Version 2.0 (the "License");
// //   you may not use this file except in compliance with the License.
// //   You may obtain a copy of the License at
// //    
// //        http://www.apache.org/licenses/LICENSE-2.0
// //    
// //   Unless required by applicable law or agreed to in writing, software
// //   distributed under the License is distributed on an "AS IS" BASIS,
// //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// //   See the License for the specific language governing permissions and
// //   limitations under the License.
// //   
// //   MODIFICATIONS HAVE BEEN MADE TO THIS FILE
using System;
using System.Collections.Specialized;
using System.IO;
using System.Security.Principal;
using System.Web;
using System.Xml.Linq;
using NUnit.Framework;

namespace Comdiv.QWeb.Test.HttpHandling {
	[TestFixture]
	public class qwebcontext_test {

		[Test]
		public void xdata_can_be_null() {
			Assert.AreEqual(null,new QWebContext().XData);
		}

		[Test]
		public void xdata_by_json()
		{
			Assert.AreEqual(@"<object x=""1"" />", new QWebContext().Set("_jdata","{ x : 1}").XData.ToString(SaveOptions.DisableFormatting));
		}

		[Test]
		public void context_logon_user_and_user() {
			var test = new GenericPrincipal(new GenericIdentity("test"), null);
			var ctx = new QWebContext {LogonUser = test};
			Assert.AreEqual("test",ctx.LogonUser.Identity.Name);
			Assert.AreEqual("test",ctx.User.Identity.Name);
			ctx = new QWebContext {Registry = new QWebServiceRegistry(),LogonUser = test};
			QWebContext.Current = ctx;
			Assert.AreEqual("test", ctx.User.Identity.Name);
			ctx = new QWebContext { Registry = new QWebServiceRegistry(), LogonUser = test };
			QWebContext.Current = null;
			Assert.AreNotEqual("test", ctx.User.Identity.Name);
		}

		[Test]
		public void xdata_by_xml()
		{
			Assert.AreEqual(@"<object x=""1"" />", new QWebContext().Set("_xdata", @"<object x=""1""/>").XData.ToString(SaveOptions.DisableFormatting));
		}

		[Test]
		public void xdata_by_bxl()
		{
			Assert.AreEqual(@"<object x=""1"" />", new QWebContext().Set("_bxdata", @"object x=1").XData.ToString(SaveOptions.DisableFormatting));
		}

		[Test]
		public void command_parsed() {
			Assert.AreEqual("start.end", new QWebContext("http://server/app/start/end.quick?param").ActionName);
			Assert.AreEqual("start.end", new QWebContext("http://server/app/start/end.json.quick?param").ActionName);
		}

		[Test]
		public void type_parsed() {
			Assert.AreEqual("js", new QWebContext("http://server/app/start/end.quick?param").RenderType);
			Assert.AreEqual("json", new QWebContext("http://server/app/start/end.json.quick?param").RenderType);
			Assert.AreEqual("wiki", new QWebContext("http://server/app/start/end.wiki.quick?param").RenderType);
		}

		[Test]
		public void works_with_httpcontext() {
			var req = new HttpRequest("", "http://server/app/start/end.wiki.quick?param", "x=1&y=2");
			var ctx = new HttpContext(req, new HttpResponse(null));
			var context = new QWebContext(new HttpContextWrapper( ctx));
			Assert.AreEqual("start.end", context.ActionName);
			Assert.AreEqual("wiki", context.RenderType);
			Assert.AreEqual("1", context.Parameters["x"]);
			Assert.AreEqual("2", context.Parameters["y"]);
		}

        [Test]
        public void multiple_name_param()
        {
            var req = new HttpRequest("", "http://server/app/start/end.wiki.quick", "y=1&y=2");
            var ctx = new HttpContext(req, new HttpResponse(null));
            var context = new QWebContext(new HttpContextWrapper(ctx));
            Assert.AreEqual("1,2", context.Parameters["y"]);
        }

		public class sampleResponse : HttpResponseBase {
			NameValueCollection _headers = new NameValueCollection();
			public override System.Collections.Specialized.NameValueCollection Headers
			{
				get { return _headers; }
			}
		}

		public class sampleContext : HttpContextBase {
			private sampleRequest _request;
			private sampleResponse _response;

			public sampleContext(sampleRequest req,sampleResponse res) {
				this._request = req;
				this._response = res;
			}
			public override HttpRequestBase Request
			{
				get { return _request; }
			}
			public override HttpResponseBase Response
			{
				get { return _response; }
			}
		}

		public class sampleRequest : HttpRequestBase {
			NameValueCollection _headers = new NameValueCollection();
			public override System.Collections.Specialized.NameValueCollection Headers
			{
				get { return _headers; }
			}
			NameValueCollection _qs = new NameValueCollection();
			public override System.Collections.Specialized.NameValueCollection QueryString
			{
				get { return _qs; }
			}
			NameValueCollection _fm = new NameValueCollection();
			public override System.Collections.Specialized.NameValueCollection Form
			{
				get { return _fm; }
			}
		}

		
	}
}